<?php


class EmsCommentTable extends Doctrine_Table
{
    public function addEnabledCommentsQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsComment c');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.is_enabled = ?', 1);

        return $q;
    }

    public function addApprovedCommentsQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsComment c');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.approved = ?', 1);

        return $q;
    }

    public function addContentCommentsQuery($content_id, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsComment c');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias.'.content_id = ?', $content_id);

        return $q;
    }

    public function addPortalCommentsQuery($portal_id, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsComment c');
        }

        $alias = $q->getRootAlias();

        $q->leftJoin($alias.'.Content cont')
                ->leftJoin('cont.Section s WITH s.portal_id = ?', $portal_id);

        return $q;
    }

    /*
     * $desc es un valor booleano que indica si la sentencia order by
     * será descendente o ascendente.
     */
    public function addOrderByCreatedAtClause($desc = true, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsComment c');
        }

        $alias = $q->getRootAlias();

        $q->addOrderBy($alias.'.created_at '.($desc ? 'DESC' : 'ASC'));

        return $q;
    }

    public function getEnabledContentCommentsQuery($content_id, $desc = true)
    {
        return $this->addOrderByCreatedAtClause($desc, $this->addEnabledCommentsQuery(
                $this->addContentCommentsQuery($content_id)));
    }

    public function getEnabledAndApprovedContentCommentsQuery($content_id, $desc = true)
    {
        return $this->addOrderByCreatedAtClause($desc, $this->addEnabledCommentsQuery(
                $this->addApprovedCommentsQuery($this->addContentCommentsQuery($content_id))));
    }

    public function getEnabledContentComments($content_id, $desc = true)
    {
        return $this->getEnabledContentCommentsQuery($content_id, $desc)->execute();
    }

    public function getEnabledPortalCommentsQuery($portal_id, $desc = true)
    {
        return $this->addOrderByCreatedAtClause($desc, $this->addEnabledCommentsQuery(
                $this->addPortalCommentsQuery($portal_id)));
    }

    public function getEnabledPortalComments($portal_id, $desc = true)
    {
        return $this->getEnabledPortalCommentsQuery($portal_id, $desc)->execute();
    }

    public static function getInstance()
    {
        return Doctrine_Core::getTable('EmsComment');
    }
}